package com.peixunp.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.peixunp.bean.News;
import com.peixunp.service.NewsService;

import finaler.util.Page;
import finaler.util.PageList;

@Controller
@RequestMapping("news")
public class NewsController {

	@Autowired
	private NewsService newsService;

	@RequestMapping( value="/index.html" )
	public String newsList(HttpServletRequest request) throws Exception {
		Page page = new Page(request);
		page.setRange(50);
		page.putParameter("newsKey", request.getParameter("newsKey"));
		PageList newsList = newsService.getNewsList(page);
		request.setAttribute("newsList", newsList);
		return "/news/list";
	}
	
	@RequestMapping( value="/{newsId}.html" )
	public String news(@PathVariable int newsId,HttpServletRequest request) throws Exception {
		News news  = newsService.findByKey(newsId);
		news.setHit(news.getHit()+1);
		newsService.update(news);
		request.setAttribute("news", news);
		return "/news/news";
	}

}
